import os
from datetime import timedelta

from flask import Flask, request, session, jsonify
from flask.views import MethodView
import config
from exts import db,mail,cors,config_app
from server.resource.User import bp as user_bp
from server.resource.test import bp as test_bp
from server.resource.coupon import bp as coupon_bp
from server.resource.MetaData import bp as MetaData_bp
from server.resource.Magager import bp as Manager_bp
from server.resource.TestReport import bp as TestReport_bp
from server.resource.ELA import bp as ELA_bp
from server.resource.DCT import bp as DCT_bp
from server.resource.A3 import bp as A3_bp
from server.resource.A4 import bp as A4_bp
from flask_migrate import Migrate
import secrets

from server.resource.util.ApiResponse import ApiResponse

from flask_jwt_extended import create_access_token, jwt_required, get_jwt_identity, JWTManager

app = Flask(__name__)
app.config['JWT_SECRET_KEY'] = '13712948040'
app.config['JWT_EXPIRATION_DELTA'] = timedelta(minutes=60*12)
jwt = JWTManager(app)

#绑定配置文件
app.config.from_object(config)
db.init_app(app)
mail.init_app(app)
cors.init_app(app, resources=r'/*', supports_credentials=True)
config_app(app)
migrate = Migrate(app,db)


# session设置
# 生成随机密钥
app.secret_key = secrets.token_hex(16)
# 使用环境变量来设置secret_key，确保它是安全的
# app.secret_key = os.environ[app.secret_key]

#注册蓝图
app.register_blueprint(user_bp)
app.register_blueprint(test_bp)
app.register_blueprint(MetaData_bp)
app.register_blueprint(coupon_bp)
app.register_blueprint(Manager_bp)
app.register_blueprint(TestReport_bp)
app.register_blueprint(ELA_bp)
app.register_blueprint(DCT_bp)
app.register_blueprint(A3_bp)
app.register_blueprint(A4_bp)

@app.route('/test',methods=['GET'])
def sess():
    session['is_login'] = 'true'
    print(session['is_login'])
    return jsonify(ApiResponse.success())
#设置拦截器
# @app.before_request
# def before():
#     """
#     针对app实例定义全局拦截器
#     """
#     url = request.path  # 读取到当前接口的地址
#     pass_list = ['/','/test','/user/login','/user/register','/user/login/?username=admin&password=123456']
#     suffix = url.endswith('.png') or url.endswith('.jpg') or url.endswith('.css') or url.endswith('.js')
#     if url in pass_list or suffix:
#         pass
#     elif session.get('is_login') != 'true':
#         print(session.get('is_login'))
#         # print(session.get('username'))
#         return '你还没有登录'
#     else:
#         pass

if __name__ == '__main__':
    app.run("0.0.0.0", debug=True, port=8080)